package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.dbmodel.GroupdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.SyncInfodbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 16/11/9.
 */

public class GroupdbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(GroupdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (CommonUtils.isEmpty(model.getTip())){
                model.setTip("1");
            }
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //设置团名称
    public static boolean setGroupName(String name,String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            GroupdbModel model = db.findById(GroupdbModel.class,id);
            if (model != null){
                model.setName(name);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取团名称
    public static String getGroupName(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupdbModel.class,id) != null)
              return db.findById(GroupdbModel.class,id).getName();
        } catch (DbException e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }

    //设置团头像
    public static boolean setGroupHeader(String header,String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            GroupdbModel model = db.findById(GroupdbModel.class,id);
            if (model != null){
                model.setHeader(header);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取团名称
    public static String getGroupHeader(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupdbModel.class,id) != null)
             return db.findById(GroupdbModel.class,id).getHeader();
        } catch (DbException e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }

    //设置团公告
    public static boolean setGroupReport(String report,String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            GroupdbModel model = db.findById(GroupdbModel.class,id);
            if (model != null){
                model.setReport(report);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取团名称
    public static String getGroupReport(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupdbModel.class,id) != null){
                return db.findById(GroupdbModel.class,id).getReport();
            }
            return "";
        } catch (DbException e) {
            e.printStackTrace();
            return "";
        }
    }

    //设置群同步时间
    public static boolean setGroupTime(String asynctime,String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            GroupdbModel model = db.findById(GroupdbModel.class,id);
            if (model != null){
                model.setAsyntime(asynctime);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取群同步时间
    public static String getAsyncTime(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupdbModel.class,id) != null)
             return db.findById(GroupdbModel.class,id).getAsyntime();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //获取群状态
    public static String getState(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupdbModel.class,id) != null)
             return db.findById(GroupdbModel.class,id).getState();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //获取所有属性
    public static GroupdbModel getModel(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.findById(GroupdbModel.class,id);
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取所有团
    public static List<GroupdbModel> getAllModel(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(GroupdbModel.class).where("userid","==",userid).and("state","!=","2") != null)
             return db.selector(GroupdbModel.class).where("userid","==",userid).and("state","!=","2").findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //获取保存到通讯录的所有团
    public static List<GroupdbModel> getAllSaveModel(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(GroupdbModel.class).where("userid","==",userid).and("state","==","1") != null)
              return db.selector(GroupdbModel.class).where("userid","==",userid).and("state","==","1").findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //设置所有团为未同步状态
    public static boolean setGroupToUnAsyncState(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(GroupdbModel.class).where("userid","==",userid).and("state","!=","2") != null){
                List<GroupdbModel> list = db.selector(GroupdbModel.class).where("userid","==",userid).and("state","!=","2").findAll();
                if (list != null && list.size() > 0){
                    for (GroupdbModel model : list){
                        model.setAsyntime("0");
                        GroupdbService.saveOrUpdate(model);
                    }
                }
            }

            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteModel(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(GroupdbModel.class,id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }

    }

    //删除数据
    public static boolean deleteModel(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(GroupdbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }

    }
}
